package com.bs.bsacexj.web.controller;



import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.bs.bsacexj.common.core.domain.AjaxResult;
import com.bs.bsacexj.domain.AccessControlDeviceInfoVO;
import com.bs.bsacexj.domain.AccessControlEventInfoVO;
import com.bs.bsacexj.service.impl.HIKSafePlatACEService;
import com.sun.istack.internal.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.bsacexj.common.StringUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/debugAC")
public class DebugAccessControlController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    HIKSafePlatACEService accessControlManager;
    @PostMapping("/selectUserInfoByCardNO")
    public AjaxResult selectUserInfoByCardNO(@NotNull String cardNO) throws Exception {
        logger.info("根据身份证号查询海康人员编号，传入身份证号为:{}",cardNO);
        String hikPersonId = accessControlManager.selectUserInfo(cardNO);
        Map<String,Object> map = new HashMap<>();
        map.put("hikPersonId",hikPersonId);
        return  AjaxResult.success(map);
    }

    @PostMapping("/selectDeviceList")
    public AjaxResult selectDeviceList(@NotNull int pageIndex,@NotNull int pageSize) throws Exception {
        logger.info("根据页码和页大小查询海康设备信息，页数为:{},页面大小为{}",pageIndex,pageSize);
        List<AccessControlDeviceInfoVO> list =accessControlManager.selectAccessControlDeviceList(pageIndex,pageSize);
        return  AjaxResult.success(list);
    }

    @PostMapping("/addDoorAuth")
    public AjaxResult addDoorAuth(@NotNull String personId,@NotNull String deviceId,@NotNull String startTime,@NotNull String endTime) throws Exception {
        logger.info("根据用户信息和设备编号，起止时间来配置权限，用户ID为:{},设备ID为",personId,deviceId);
        String pattern = "yyyy-MM-dd HH:mm:ss";
        Date startDate= DateUtil.parse(startTime,pattern);
        Date endDate= DateUtil.parse(endTime,pattern);
        Boolean result=accessControlManager.addAuthConfig(personId,deviceId,startDate,endDate);
        if(result){
            return  AjaxResult.success();
        }else {
            return  AjaxResult.error();
        }
    }

    @PostMapping("/deletDooreAuth")
    public AjaxResult deletDooreAuth(@NotNull String personId,@NotNull String deviceId) throws Exception {
        logger.info("根据用户信息和设备编号，起止时间来删除权限，用户ID为:{},设备ID为",personId,deviceId);

        Boolean result=accessControlManager.deleteAuthConfig(personId,deviceId);
        if(result){
            return  AjaxResult.success();
        }else {
            return  AjaxResult.error();
        }
    }


    @PostMapping("/deviceShortcut")
    public AjaxResult deviceShortcut(@NotNull String deviceId) throws Exception {
        logger.info("执行下发策略，设备编号为:{}",deviceId);
        Boolean result= accessControlManager.configurationShortcut(deviceId);;
        if(result){
            return  AjaxResult.success();
        }else {
            return  AjaxResult.error();
        }
    }

    private static  final String[] productionAreaDeviceIds ={"678ffdb5644a4b058abb4d00265980a4"};
    private static  final String[] officeAreaDeviceIds ={"4607af933ab641519de165fecbd8bf4e"};
    @PostMapping("/editPersonnelPermissionsForEquipment")
    public AjaxResult editPersonnelPermissionsForConfiguringEquipment(@NotNull String cardNO,@NotNull int areaType,@NotNull int commandType, @NotNull String startTime,@NotNull String endTime) throws Exception {
        //STEP1 根据身份证号查询人员编号
        String hikPersonId = accessControlManager.selectUserInfo(cardNO);
        if(StringUtils.isEmpty(hikPersonId)){
            return AjaxResult.error("查询不到相关人员信息!");
        }
        //STEP2 配置权限
        String deviceId="";
        if(areaType==1){
            deviceId=productionAreaDeviceIds[0];
        }else if(areaType==2){
            deviceId=officeAreaDeviceIds[0];
        }
        String pattern = "yyyy-MM-dd HH:mm:ss";
        Date startDate= DateUtil.parse(startTime,pattern);
        Date endDate= DateUtil.parse(endTime,pattern);
        Boolean result=false;
        if(commandType==1){
            result=accessControlManager.addAuthConfig(hikPersonId,deviceId,startDate,endDate);
        }else if(commandType==0) {
            result=accessControlManager.deleteAuthConfig(hikPersonId,deviceId);
        }
        //STEP3 通知设备下载配置
        if(result){
            result=accessControlManager.configurationShortcut(deviceId);
        }
        if(result){
            return  AjaxResult.success();
        }else {
            return  AjaxResult.error();
        }
    }

    //查询门禁点事件
    @PostMapping("/selectAccessControlEvent")
    public AjaxResult selectAccessControlEvent( @NotNull String startTime,@NotNull String endTime,@NotNull int pageIndex,@NotNull int pageSize) throws Exception {
        List<AccessControlEventInfoVO> list=accessControlManager.selectAccessControlEventList(startTime,endTime,pageIndex,pageSize);
        if(CollectionUtil.isNotEmpty(list)){
            return AjaxResult.success(list);
        }else {
            return AjaxResult.error();
        }
    }


}